package com.coollang.smartbra.activity;

import android.app.ProgressDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.MyAutoLayoutActivity;
import com.coollang.smartbra.R;
import com.coollang.smartbra.adapter.UMoneyDetailsAdapter;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.model.UMoneyDetailsModel;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.Urls;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author Timmy
 *         U币明细
 */
public class UMoneyDetailsActivity extends MyAutoLayoutActivity {

    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycleiew)
    RecyclerView mRecyclerView;
    private Gson mGson;
    private UMoneyDetailsAdapter mDetailsAdapter;
    private ProgressDialog mProgressDialog;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_umoney_details);
        ButterKnife.bind(this);
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);
        initViews();
    }

    private void initViews() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivRight.setVisibility(View.GONE);
        tvTitle.setText("U币明细");
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_80));

        mDetailsAdapter = new UMoneyDetailsAdapter(this);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mDetailsAdapter);
    }

    @Override
    protected void initData() {
        showProgressDialog("", "正在加载数据..");
        mGson = new Gson();
        loadMoneyDetails();
    }

    /**
     * 网络请求加载数据
     */
    private void loadMoneyDetails() {
        MyHttp.getInstance().getCoinRecord(new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                UMoneyDetailsModel model = mGson.fromJson(results, UMoneyDetailsModel.class);

                int code = model.getCode();
                if (code == Urls.CODE_SUCCESS) {
                    List<UMoneyDetailsModel.DataBean> data = model.getData();

                    /**
                     * 去除金币是0的 和未知运动
                     */
                    Iterator<UMoneyDetailsModel.DataBean> iterator = data.iterator();
                    while (iterator.hasNext()) {
                        UMoneyDetailsModel.DataBean next = iterator.next();
                        int coinCount = Integer.parseInt(next.getCoinCount());
                        int sportCount = Integer.parseInt(next.getSportCount());
                        String sportName = next.getSportName().trim();

                        if (coinCount == 0) {
                            iterator.remove();
                        } else if (TextUtils.isEmpty(sportName)) {
                            iterator.remove();
                        } else if (sportCount == 0) {
                            iterator.remove();
                        }
                    }

                    Collections.reverse(data);
                    mDetailsAdapter.setDataBeanList(data);
                } else {
                    MyHttp.getInstance().showErrorToast(UMoneyDetailsActivity.this, code);
                }
                hideProgressDialog();
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                hideProgressDialog();
                MyHttp.getInstance().showErrorToast(UMoneyDetailsActivity.this, Urls.CODE_Error_);
            }
        });
    }

    private void showProgressDialog(String title, String message) {

        if (mProgressDialog == null) {
            mProgressDialog = ProgressDialog.show(UMoneyDetailsActivity.this, title, message, false);
        } else if (mProgressDialog.isShowing()) {
            mProgressDialog.setTitle(title);
            mProgressDialog.setMessage(message);
        }
        mProgressDialog.show();
    }

    private void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }
}
